package org.tis.tools.abf.module.ac.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.abf.module.ac.controller.request.AcDeleteListRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorAddRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorSetPassWorldRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorStatusRequest;
import org.tis.tools.abf.module.ac.dao.AcOperatorMapper;
import org.tis.tools.abf.module.ac.entity.*;
import org.tis.tools.abf.module.ac.entity.enums.AuthMode;
import org.tis.tools.abf.module.ac.entity.enums.OperatorStatus;
import org.tis.tools.abf.module.ac.entity.vo.AcOperatorMenuDetail;
import org.tis.tools.abf.module.ac.entity.vo.AcOperatorMenuUiEntityDetail;
import org.tis.tools.abf.module.ac.exception.AcExceptionCodes;
import org.tis.tools.abf.module.ac.exception.AcOperatorManagementException;
import org.tis.tools.abf.module.ac.exception.AcRoleManagementException;
import org.tis.tools.abf.module.ac.service.*;
import org.tis.tools.abf.module.common.entity.enums.YON;
import org.tis.tools.abf.module.om.service.IOmEmployeeService;
import org.tis.tools.core.utils.BeanFieldValidateUtil;
import org.tis.tools.core.utils.StringUtil;
import org.tis.tools.model.exception.i18.ExceptionCodes;

import java.math.BigDecimal;
import java.util.*;

import static org.tis.tools.core.utils.BasicUtil.wrap;

/**
 * acOperator的Service接口实现类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AcOperatorServiceImpl extends ServiceImpl<AcOperatorMapper, AcOperator> implements IAcOperatorService {

    @Autowired
    private IAcOperatorService acOperatorService;
    @Autowired
    private IAcOperatorRoleService acOperatorRoleService;
    @Autowired
    private IAcOperatorFuncService acOperatorFuncService;
    @Autowired
    private IAcOperatorConfigService acOperatorConfigService;
    @Autowired
    private IAcOperatorIdentityService acOperatorIdentityService;
    @Autowired
    private IAcOperatorMenuService acOperatorMenuService;
    @Autowired
    private IAcOperatorShortcutService acOperatorShortcutService;
    @Autowired
    private IAcMenuService acMenuService;
    @Autowired
    private IAcFuncService acFuncService;
    @Autowired
    private IOmEmployeeService omEmployeeService;


    /**
    *  新增操作员
    */
    @Override
    public void addAcOperator(AcOperatorAddRequest request) throws AcOperatorManagementException {

        AcOperator acOperator = new AcOperator();
        //操作员状态默认是停用
        acOperator.setOperatorStatus(OperatorStatus.stop);
        //锁定次数前端不传值默认为5,否则为前段值
        if ("".equals(request.getLockLimit())){
            acOperator.setLockLimit(BigDecimal.valueOf(5));
        }else {
            acOperator.setLockLimit(request.getLockLimit());
        }

        if (null == request.getAuthMode()){
            acOperator.setAuthMode(AuthMode.password);
            acOperator.setPassword("111111");
        }else {
            acOperator.setAuthMode(request.getAuthMode());
            acOperator.setPassword(request.getPassword());
        }
        //设置当前错误登录次数为0
        acOperator.setErrCount(new BigDecimal("0"));

        //收集参数
        acOperator.setUserId(request.getUserId());
        acOperator.setOperatorName(request.getOperatorName());
        acOperator.setInvalDate(request.getInvalDate());
        acOperator.setLockTime(request.getLockTime());
        acOperator.setUnlockTime(request.getUnlockTime());
        acOperator.setLastLogin(request.getLastLogin());
        acOperator.setStartDate(request.getStartDate());
        acOperator.setEndDate(request.getEndDate());
        acOperator.setValidTime(request.getValidTime());
        acOperator.setMacCode(request.getMacCode());
        acOperator.setIpAddress(request.getIpAddress());

        insert(acOperator);

        //新增 默认操作员身份
        AcOperatorIdentity acOperatorIdentity = new AcOperatorIdentity();
        //固定名称
        acOperatorIdentity.setIdentityName("系统默认身份");
        acOperatorIdentity.setIdentityFlag(YON.YES);
        acOperatorIdentity.setGuidOperator(acOperator.getGuid());
        acOperatorIdentityService.insert(acOperatorIdentity);

    }

    /**
     * 修改操作员结果
     */
    @Override
    public AcOperator updateAcOperatorByCondition(AcOperator acOperator) throws AcOperatorManagementException {


        //判断传入的对象是否为空
        if (acOperator == null) {
            throw new AcOperatorManagementException(AcExceptionCodes.OBJECT_IS_NULL, wrap("acOperator"));
        }
        // TODO 验证不全
        String[] validateFields = {
                "guid", "operatorName", "authMode"
        };
        try {
            String result = BeanFieldValidateUtil.checkObjFieldRequired(acOperator, validateFields);
            //USER_ID 必填
            if (!StringUtil.isBlank(result)) {
                throw new AcOperatorManagementException(ExceptionCodes.LACK_PARAMETERS_WHEN_UPDATE);
            }
        }catch (Exception e){
            throw new AcOperatorManagementException(ExceptionCodes.LACK_PARAMETERS_WHEN_UPDATE);
        }

        updateById(acOperator);
        return acOperator;
    }


    @Override
    public AcOperator changeOperatorStatus(AcOperatorStatusRequest acOperatorStatusRequest) throws AcOperatorManagementException {

        /*判断改变操作员状态时是否合法*/
        //获取当前操作员状态
        AcOperator acOperator = selectById(acOperatorStatusRequest.getGuid());
        OperatorStatus operatorStatusNow = acOperator.getOperatorStatus();
        OperatorStatus operatorStatusChange = acOperatorStatusRequest.getOperatorStatus();

        switch (operatorStatusNow){
            case stop:
                if (!(operatorStatusChange.equals(OperatorStatus.logout) || operatorStatusChange.equals(OperatorStatus.clear) ||operatorStatusChange.equals(OperatorStatus.delete))){
                    throw new AcOperatorManagementException(AcExceptionCodes.STOP_STATUS_IS_NOT_ALLOWED_CHANGE,
                            wrap("停用状态只能修改为退出,删除或注销状态"));
                }
                break;
            case logout:
                if (!(operatorStatusChange.equals(OperatorStatus.login) || operatorStatusChange.equals(OperatorStatus.clear))){
                    throw new AcOperatorManagementException(AcExceptionCodes.LOGOUT_STATUS_IS_NOT_ALLOWED_CHANGE,
                            wrap("退出状态只能修改为正常状态或注销状态"));
                }
                break;
            case login:
                if (!(operatorStatusChange.equals(OperatorStatus.logout) || operatorStatusChange.equals(OperatorStatus.pause))){
                    throw new AcOperatorManagementException(AcExceptionCodes.LOGIN_STATUS_IS_NOT_ALLOWED_CHANGE,
                            wrap("正常状态只能修改为退出状态或挂起状态"));
                }
                break;
            case clear:
                if (!(operatorStatusChange.equals(OperatorStatus.stop))){
                    throw new AcOperatorManagementException(AcExceptionCodes.CLEAR_STATUS_IS_NOT_ALLOWED_CHANGE,
                            wrap("注销状态只能修改为停用状态"));
                }
                break;
            case lock:
                if (!(operatorStatusChange.equals(OperatorStatus.logout) || operatorStatusChange.equals(OperatorStatus.clear))){
                    throw new AcOperatorManagementException(AcExceptionCodes.LOCK_STATUS_IS_NOT_ALLOWED_CHANGE,
                            wrap("锁定状态只能修改为退出状态和注销状态"));
                }
                break;
            case pause:
                if (!(operatorStatusChange.equals(OperatorStatus.logout) || operatorStatusChange.equals(OperatorStatus.login))){
                    throw new AcOperatorManagementException(AcExceptionCodes.PAUSE_STATUS_IS_NOT_ALLOWED_CHANGE,
                            wrap("挂起状态只能修改为退出状态和正常状态"));
                }
                break;
            default:
                    throw new AcOperatorManagementException(AcExceptionCodes.CURRENT_STATUS_IS_NOT_ALLOWED_CHANGE,
                            wrap("该状态已为操作员终止状态!"));
        }
        acOperator.setOperatorStatus(operatorStatusChange);
        if (operatorStatusNow.equals(OperatorStatus.lock) && operatorStatusChange.equals(OperatorStatus.logout)){
            //设置当前错误登录次数为0
            acOperator.setErrCount(new BigDecimal("0"));
        }
        updateById(acOperator);

        return acOperator;
    }

    /**
     * 查询所有操作员
     */
    @Override
    public List<AcOperator> queryAllOperator() throws AcOperatorManagementException {

        Wrapper<AcOperator> wrapper = new EntityWrapper<AcOperator>();

        return selectList(wrapper);
    }


    @Override
    public void moveOperator(String id) throws AcOperatorManagementException {

        AcOperator acOperator = selectById(id);

        if(!acOperator.getOperatorStatus().equals(OperatorStatus.stop)){
            throw new AcOperatorManagementException(AcExceptionCodes.CURRENT_STATUS_IS_NOT_ALLOWED_DELETE,
                    wrap(acOperator.getOperatorStatus(), AcOperator.NAME));
        }

        //删除操作员与权限集（角色）对应关系数据
        Wrapper<AcOperatorRole> wrapperRole = new EntityWrapper<>();
        wrapperRole.eq(AcOperatorRole.COLUMN_GUID_OPERATOR,id);
        List<AcOperatorRole> listRole = acOperatorRoleService.selectList(wrapperRole);
        if (0 != listRole.size()){
            for (AcOperatorRole acOperatorRole : listRole){
                if (null != acOperatorRole){
                    acOperatorRoleService.deleteOperatorRole(acOperatorRole.getGuid());
                }
            }
        }

        //删除操作员特殊权限配置数据
        Wrapper<AcOperatorFunc> wrapperFunc = new EntityWrapper<>();
        wrapperFunc.eq(AcOperatorFunc.COLUMN_GUID_OPERATOR,id);
        List<AcOperatorFunc> listFunc = acOperatorFuncService.selectList(wrapperFunc);
        if (0 != listFunc.size()){
            for (AcOperatorFunc acOperatorFunc : listFunc){
                if (null != acOperatorFunc){
                    acOperatorFuncService.deleteOperatorFunc(acOperatorFunc.getGuid());
                }
            }
        }

        //删除操作员个性配置数据
        Wrapper<AcOperatorConfig> wrapperConfig = new EntityWrapper<>();
        wrapperConfig.eq(AcOperatorConfig.COLUMN_GUID_OPERATOR,id);
        List<AcOperatorConfig> listConfig = acOperatorConfigService.selectList(wrapperConfig);
        if (0 != listConfig.size()){
            for (AcOperatorConfig acOperatorConfig : listConfig){
                if (null != acOperatorConfig){
                    acOperatorConfigService.deleteOperatorConfig(acOperatorConfig.getGuid());
                }
            }
        }

        //删除操作员重组菜单数据
        Wrapper<AcOperatorMenu> wrapperMenu = new EntityWrapper<>();
        wrapperMenu.eq(AcOperatorMenu.COLUMN_GUID_OPERATOR,id);
        List<AcOperatorMenu> listMenu = acOperatorMenuService.selectList(wrapperMenu);
        if (0 != listMenu.size()){
            acOperatorMenuService.delete(wrapperMenu);
        }

        //删除操作员快捷菜单数据
        Wrapper<AcOperatorShortcut> wrapperShort = new EntityWrapper<>();
        wrapperShort.eq(AcOperatorShortcut.COLUMN_GUID_OPERATOR,id);
        List<AcOperatorShortcut> listShort = acOperatorShortcutService.selectList(wrapperShort);
        if (0 != listShort.size()){
            acOperatorShortcutService.delete(wrapperShort);
        }

        //删除操作员身份数据
        Wrapper<AcOperatorIdentity> wrapperIdentity = new EntityWrapper<>();
        wrapperIdentity.eq(AcOperatorIdentity.COLUMN_GUID_OPERATOR,id);
        List<AcOperatorIdentity> listIdentity = acOperatorIdentityService.selectList(wrapperIdentity);
        if (0 != listIdentity.size()){
           for (AcOperatorIdentity acOperatorIdentity : listIdentity){
               if (null != acOperatorIdentity){
                   acOperatorIdentityService.moveIdentity(acOperatorIdentity.getGuid());
               }
           }
        }

        //删除操作员对应的员工
//        Wrapper<OmEmployee> wrapperEmp = new EntityWrapper<>();
//        wrapperEmp.eq(OmEmployee.COLUMN_GUID_OPERATOR,id);
//        List<OmEmployee> listEmps = omEmployeeService.selectList(wrapperEmp);
//        if (0 != listEmps.size()){
//            for (OmEmployee omEmployee : listEmps) {
//                if (null != omEmployee){
//                    omEmployeeService.moveEmp(omEmployee.getGuid());
//                }
//            }
//        }

        //删除操作员
        deleteById(id);
    }

    /**
     * 批量删除操作员
     */
    @Override
    public void batchDelete(AcDeleteListRequest listRequest) throws AcOperatorManagementException {

        if (0 == listRequest.getDeleteList().size()){
            throw new AcOperatorManagementException(AcExceptionCodes.PARAMETER_IS_EMPTY_WHRN_BATCH_DELETE,wrap("删除时参数列表为空"));
        }
        for (String operatorGuid : listRequest.getDeleteList()){
            if (!StringUtil.isEmpty(operatorGuid)){
                AcOperator acOperator = selectById(operatorGuid);
                if (null == acOperator){
                    throw new AcRoleManagementException(AcExceptionCodes.FAILURE_WHEN_QUERY_AC_OPERATOR,wrap
                            ("删除的列表中有不存在或已删除的操作员！"));
                }
            }
        }

        for(String operatorGuid : listRequest.getDeleteList()){
            if (!StringUtil.isEmpty(operatorGuid)){
                moveOperator(operatorGuid);
            }
        }
    }

    /**
     * 根据条件查询角色下的操作员
     */
    @Override
    public Page<AcOperator> queryByRole(String roleId, Page<AcOperator> page, EntityWrapper<AcOperator> wrapper) throws AcOperatorManagementException {

        List<String> operatorGuidList = this.baseMapper.queryOperatorGuidByRole(roleId);

        Page<AcOperator> pageQue = new Page<AcOperator>();
        if (null == wrapper){
            wrapper = new EntityWrapper<AcOperator>();
        }
        if (0 != operatorGuidList.size()){
            wrapper.in(AcOperator.COLUMN_GUID,operatorGuidList);
            pageQue = selectPage(page,wrapper);
        }

        return pageQue;
    }


    /**
     * 修改密码
     */
    @Override
    public AcOperator changePassword(AcOperator acOperator) throws AcOperatorManagementException {

        AcOperator acOperatorQue = selectById(acOperator.getGuid());

        acOperatorQue.setPassword(acOperator.getPassword());

        updateById(acOperatorQue);
        return acOperatorQue;
    }


    @Override
    public void changePasswordApi(AcOperator acOperator,AcOperatorSetPassWorldRequest passWorldRequest) throws AcOperatorManagementException {

        acOperator.setPassword(passWorldRequest.getNewFirstPassword());

        updateById(acOperator);
    }

    /**
     * 重置密码
     */
    @Override
    public AcOperator resetPassword(String id) throws AcOperatorManagementException {
        AcOperator acOperatorQue = selectById(id);

        acOperatorQue.setPassword("111111");

        updateById(acOperatorQue);
        return acOperatorQue;
    }

    /**
     * 员工入职时绑定操作员,查询所有未被绑定的操作员
     */
    @Override
    public List<AcOperator> queryOperatorForEmployee() throws AcOperatorManagementException {

        List<AcOperator> operatorList = new ArrayList<AcOperator>();

        operatorList = this.baseMapper.queryOperatorForEmployee();
        return operatorList;
    }

    /**
     * 根据userId查询操作员
     */
    @Override
    public AcOperator queryByUserId(String userId) throws AcOperatorManagementException {

        Wrapper<AcOperator> wrapper = new EntityWrapper<AcOperator>();
        wrapper.eq(AcOperator.COLUMN_USER_ID,userId);

        return selectOne(wrapper);
    }

    /**
     * 对外提供查询操作员的行为
     */
    @Override
    public Map<String, List<String>> queryBehavesByOperator(AcOperator acOperator) throws AcOperatorManagementException {

        //查询操作员绑定角色的所有行为,默认授权为特别允许
        List<String> behaveByRoleList = this.baseMapper.queryBehaveByRole(acOperator.getGuid());

        //查询操作员绑定的行为,授权为特别禁止
//        List<String> prohibitBehaveList = this.baseMapper.queryBehaveByProHibit(acOperator.getGuid());
        //查询操作员绑定的行为,授权为特别允许
        List<String> permitBeahvefList = this.baseMapper.queryBehaveByPermit(acOperator.getGuid());
        //查询不许验证的功能下的行为
        List<String> behaveInNoCkeckFuncs = this.baseMapper.queryBehaveInNoCkeckFunc();

        //将特别允许的两个集合合并
        permitBeahvefList.addAll(behaveByRoleList);
        permitBeahvefList.addAll(behaveInNoCkeckFuncs);
        LinkedHashSet<String> set = new LinkedHashSet<>(permitBeahvefList.size());
        set.addAll(permitBeahvefList);
        permitBeahvefList.clear();
        permitBeahvefList.addAll(set);

        Map<String,List<String>> map = new HashMap<>();
        map.put("permitBehave",permitBeahvefList);
//        map.put("prohibitBehave",prohibitBehaveList);

        return map;
    }

    /**
     * 对外提供查询操作员的菜单,行为,个性化配置
     */
    @Override
    public Map<String,Object> queryMenuByOperator(AcOperator acOperator) throws AcOperatorManagementException {

        //查询操作员的功能,特别禁止
//        List<String> prohibitFuncList = this.baseMapper.queryFuncByProHibit(acOperator.getGuid());
        //查询操作员的功能,特别允许
        List<String> permitFuncList = this.baseMapper.queryFuncByPermit(acOperator.getGuid());
        List<String> permitFuncGuidList = this.baseMapper.queryFuncGuidByPermit(acOperator.getGuid());
        //查询操作员绑定角色的所有功能,默认授权为特别允许
        List<String> funcByRoleList = this.baseMapper.queryFuncByRole(acOperator.getGuid());
        List<String> funcGuidByRoleList = this.baseMapper.queryFuncGuidByRole(acOperator.getGuid());
        //查询出不需验证的功能
        List<String> noCkeckFuncList = this.baseMapper.queryFuncNoCkeck();
//        List<String> noCkeckFuncGuidList = this.baseMapper.queryFuncGuidNoCheck();


        //将特别允许的两个集合合并
        permitFuncList.addAll(funcByRoleList);
        permitFuncList.addAll(noCkeckFuncList);
        LinkedHashSet<String> set = new LinkedHashSet<>(permitFuncList.size());
        set.addAll(permitFuncList);
        permitFuncList.clear();
        permitFuncList.addAll(set);


        //将特别允许的两个集合的GUID合并
        permitFuncGuidList.addAll(funcGuidByRoleList);
//        permitFuncGuidList.addAll(noCkeckFuncGuidList);
        LinkedHashSet<String> setGuid = new LinkedHashSet<>(permitFuncGuidList.size());
        setGuid.addAll(permitFuncGuidList);
        permitFuncGuidList.clear();
        permitFuncGuidList.addAll(setGuid);

        //行为
        Map<String, List<String>> behaveMap = queryBehavesByOperator(acOperator);

        Map<String,Object> map = new HashMap<>();

        map.put("permitBehave",behaveMap.get("permitBehave"));
//        map.put("prohibitBehave",behaveMap.get("prohibitBehave"));
        map.put("permitFunc",permitFuncList);
//        map.put("prohibitFunc",prohibitFuncList);
        map.put("operator",acOperator);

        //菜单,首先获取操作员重组菜单中的菜单,如果为空则查看操作员对应应用中的菜单
        List<AcOperatorMenu> operatorMenuList = this.baseMapper.queryMenuByOperator(acOperator.getGuid());
        LinkedList<AcOperatorMenuUiEntityDetail> uiEntityDetailList = getOperatorMenuUiEntity(operatorMenuList);
        map.put("menu",uiEntityDetailList);
        List<AcMenu> menuList = new LinkedList<>();
        if (0 == operatorMenuList.size()){
            Wrapper<AcMenu> menuWrapper = new EntityWrapper<>();
            if (0 != permitFuncGuidList.size()){
                menuWrapper.in(AcMenu.COLUMN_GUID_FUNC,permitFuncGuidList);
                menuWrapper.orderBy(AcMenu.COLUMN_DISPLAY_ORDER,true);
                menuList = acMenuService.selectList(menuWrapper);
                LinkedList<AcOperatorMenuUiEntityDetail> uiEntityDetailLinkedList = getAcMenuUiEntity(menuList);
                map.put("menu",uiEntityDetailLinkedList);
            }
        }


        //获取当前菜单结构
        List<AcOperatorMenuDetail> childrenList = new LinkedList<>();
        if (0 != operatorMenuList.size()){

            //查询AcOperatorMenu的所有菜单
            List<AcOperatorMenu> allList = this.baseMapper.queryAllOperatorMenu();

            Map<String,AcOperatorMenu> allMap = new LinkedHashMap<>();
            if (0 != allList.size()){
                //查询结果非空,则循环拼接
                for (AcOperatorMenu acOperatorMenu : allList){
                    if (null != acOperatorMenu){
                        allMap.put(acOperatorMenu.getGuid(),acOperatorMenu);
                    }
                }
            }

            //找出所有需要排序的节点信息
            for (int i = 0; i < operatorMenuList.size(); i++){
                if (null != operatorMenuList.get(i).getGuidParents()){
                    AcOperatorMenu  acMenuSon = operatorMenuList.get(i);
                    AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetailSon = getOperatorMenuUiEntity(acMenuSon);
                    AcOperatorMenuDetail acOperatorMenuSon = new AcOperatorMenuDetail(acOperatorMenuUiEntityDetailSon, null);
                    childrenList.add(acOperatorMenuSon);
                    AcOperatorMenu acMenuFather = acMenuSon;
                    while(null != allMap.get(acMenuSon.getGuidParents()).getGuidParents()) {
                        acMenuFather = allMap.get(acMenuSon.getGuidParents());
                        AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetailFather = getOperatorMenuUiEntity(acMenuFather);
                        AcOperatorMenuDetail acOperatorMenuFather = new AcOperatorMenuDetail(acOperatorMenuUiEntityDetailFather, null);
                        childrenList.add(acOperatorMenuFather);
                        acMenuSon = acMenuFather;
                    }
                    AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetailFather = getOperatorMenuUiEntity(allMap.get(acMenuFather.getGuidParents()));
                    childrenList.add(new AcOperatorMenuDetail(acOperatorMenuUiEntityDetailFather,null));
                }
            }
        }else {
            if (0 != menuList.size()) {

                //查询AcOperatorMenu的所有菜单
                List<AcMenu> allMenuList = this.baseMapper.queryAllMenu();

                Map<String, AcMenu> allMenuMap = new LinkedHashMap<>();
                if (0 != allMenuList.size()) {
                    //查询结果非空,则循环拼接
                    for (AcMenu acMenu : allMenuList) {
                        if (null != acMenu) {
                            allMenuMap.put(acMenu.getGuid(), acMenu);
                        }
                    }
                }

                //找出所有需要排序的节点信息
                for (int i = 0; i < menuList.size(); i++) {
                    if (null != menuList.get(i).getGuidParents()) {
                        AcMenu acMenuSon = menuList.get(i);
                        AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetailSon = getOperatorMenuUiEntity(acMenuSon);
                        AcOperatorMenuDetail acOperatorMenuSon = new AcOperatorMenuDetail(acOperatorMenuUiEntityDetailSon, null);
                        childrenList.add(acOperatorMenuSon);
                        AcMenu acMenuFather = acMenuSon;
                        if(null != (allMenuMap.get(acMenuFather.getGuidParents()))){
                            while (null != allMenuMap.get(acMenuSon.getGuidParents()).getGuidParents()) {
                                acMenuFather = allMenuMap.get(acMenuSon.getGuidParents());
                                AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetailFather = getOperatorMenuUiEntity(acMenuFather);
                                AcOperatorMenuDetail acOperatorMenuFather = new AcOperatorMenuDetail(acOperatorMenuUiEntityDetailFather, null);
                                childrenList.add(acOperatorMenuFather);
                                acMenuSon = acMenuFather;
                            }
                            AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetailFather = getOperatorMenuUiEntity(allMenuMap.get(acMenuFather.getGuidParents()));
                            childrenList.add(new AcOperatorMenuDetail(acOperatorMenuUiEntityDetailFather, null));
                        }
                    }
                }
            }
        }

        //为childrenList排序
        Collections.sort(childrenList, new Comparator<AcOperatorMenuDetail>() {

            @Override
            public int compare(AcOperatorMenuDetail o1, AcOperatorMenuDetail o2) {
                int num = o1.getDisplayOrder() - o2.getDisplayOrder();
                if (num > 0){
                    return 1;
                }else if (num < 0){
                    return -1;
                }
                return 0;
            }
        });

        List<AcOperatorMenuDetail> operatorMenuDetailList = new LinkedList<>();

        if (0 != childrenList.size()){
            operatorMenuDetailList = setInAcMenuDetail(childrenList);
        }

        map.put("menu",operatorMenuDetailList);
        return map;
    }

    private List<AcOperatorMenuDetail> setInAcMenuDetail(List<AcOperatorMenuDetail> resultList){

        List<AcOperatorMenuDetail> operatorMenuDetailList = new LinkedList<>();

        Map<String,AcOperatorMenuDetail> map = new LinkedHashMap<>();
        for (AcOperatorMenuDetail acOperatorMenuDetail : resultList){
            if (null != acOperatorMenuDetail){
                map.put(acOperatorMenuDetail.getGuid(),new AcOperatorMenuDetail(acOperatorMenuDetail,new ArrayList<>()));
            }
        }


        Map<String,AcOperatorMenuDetail> menuDetailMap = new LinkedHashMap<>();
        for(String key:map.keySet())
        {
            if (null != map.get(map.get(key).getGuidParents())){
                map.get(map.get(key).getGuidParents()).getChildren().add(map.get(map.get(key).getGuid()));
            }else {
                AcOperatorMenuDetail acOperatorMenuDetail = new AcOperatorMenuDetail();
                acOperatorMenuDetail = map.get(map.get(key).getGuid());
                menuDetailMap.put(acOperatorMenuDetail.getGuid(),acOperatorMenuDetail);
            }

            if (!map.keySet().contains(map.get(key).getGuidParents())){
                AcOperatorMenuDetail acOperatorMenuDetail = new AcOperatorMenuDetail();
                acOperatorMenuDetail = map.get(map.get(key).getGuid());
                menuDetailMap.put(acOperatorMenuDetail.getGuid(),acOperatorMenuDetail);
            }

        }

        if (0 != menuDetailMap.size()){
            for (String key : menuDetailMap.keySet()){
                if (null != menuDetailMap.get(key)){
                    operatorMenuDetailList.add(menuDetailMap.get(key));
                }
            }
        }

        return operatorMenuDetailList;
    }

    private LinkedList<AcOperatorMenuUiEntityDetail> getOperatorMenuUiEntity(List<AcOperatorMenu> operatorMenuList){

        LinkedList<AcOperatorMenuUiEntityDetail> list = new LinkedList<>();

        if (0 == operatorMenuList.size()){
            return list;
        }

        for (AcOperatorMenu acOperatorMenu : operatorMenuList){
            if (null != acOperatorMenu){
                String uiEntity = "";
                if (StringUtil.isNotEmpty(acOperatorMenu.getGuidFunc())){
                    AcFunc acFunc = acFuncService.selectById(acOperatorMenu.getGuidFunc());
                    uiEntity = acFunc.getUiEntry();
                }
                list.add(new AcOperatorMenuUiEntityDetail(acOperatorMenu,uiEntity));
            }
        }

        return list;
    }

    private LinkedList<AcOperatorMenuUiEntityDetail> getAcMenuUiEntity(List<AcMenu> menuList){

        LinkedList<AcOperatorMenuUiEntityDetail> list = new LinkedList<>();

        if (0 == menuList.size()){
            return list;
        }

        for (AcMenu acMenu : menuList){
            if (null != acMenu){
                String uiEntity = "";
                if (StringUtil.isNotEmpty(acMenu.getGuidFunc())){
                    AcFunc acFunc = acFuncService.selectById(acMenu.getGuidFunc());
                    uiEntity = acFunc.getUiEntry();
                }
                list.add(new AcOperatorMenuUiEntityDetail(acMenu,uiEntity));
            }
        }

        return list;
    }

    private AcOperatorMenuUiEntityDetail getOperatorMenuUiEntity(AcOperatorMenu acOperatorMenu){

        if (null == acOperatorMenu){
            return new AcOperatorMenuUiEntityDetail();
        }

        String uiEntry = "";
        if (StringUtil.isNotEmpty(acOperatorMenu.getGuidFunc())){
            AcFunc acFunc = acFuncService.selectById(acOperatorMenu.getGuidFunc());
            if (null != acFunc){
                uiEntry = acFunc.getUiEntry();
            }
        }
        AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetail = new AcOperatorMenuUiEntityDetail(acOperatorMenu,uiEntry);

        return acOperatorMenuUiEntityDetail;
    }

    private AcOperatorMenuUiEntityDetail getOperatorMenuUiEntity(AcMenu acMenu){

        if (null == acMenu){
            return new AcOperatorMenuUiEntityDetail();
        }

        String uiEntry = "";
        if (StringUtil.isNotEmpty(acMenu.getGuidFunc())){
            AcFunc acFunc = acFuncService.selectById(acMenu.getGuidFunc());
            if (null != acFunc){
                uiEntry = acFunc.getUiEntry();
            }
        }
        AcOperatorMenuUiEntityDetail acOperatorMenuUiEntityDetail = new AcOperatorMenuUiEntityDetail(acMenu,uiEntry);

        return acOperatorMenuUiEntityDetail;
    }

    /**
     * 新增时判断用户名是存在
     */
    @Override
    public Boolean existAddUserId(String userId) throws AcOperatorManagementException {

        Wrapper<AcOperator> wrapper = new EntityWrapper<>();
        wrapper.eq(AcOperator.COLUMN_USER_ID,userId);

        AcOperator acOperator = selectOne(wrapper);
        if ( null != acOperator){
            return true;
        }

        return false;
    }

    /**
     * 修改时判断用户名是存在
     */
    @Override
    public Boolean existUpdateUserId(String userId, String guid) throws AcOperatorManagementException {

        AcOperator acOperatorQue = selectById(guid);
        if (null == acOperatorQue){
            throw new AcOperatorManagementException(AcExceptionCodes.FAILURE_WHEN_QUERY_AC_OPERATOR);
        }

        Wrapper<AcOperator> wrapper = new EntityWrapper<>();
        wrapper.eq(AcOperator.COLUMN_USER_ID,userId);

        AcOperator acOperator = selectOne(wrapper);
        if ( null != acOperator){
            if (!acOperator.getGuid().equals(acOperatorQue.getGuid())){
                return true;
            }
        }

        return false;
    }
}

